package Thread;

public class Train implements Runnable{
    int total=100;
    int num=0;
    boolean flag=false;
    public void run() {

       while(!flag){
           sale();
       }

    }

    public synchronized void sale(){
        if(total<=0){
            flag=true;
            System.out.println(Thread.currentThread().getName()+"票已经售完，停止售票");
            return;
        }
        total--;
        num++;
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"正在售票，已售出"+num+"张票"+"剩余"+total+"张票");
    }
}
